﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
using System.Drawing.Drawing2D;

namespace DrawTools
{
    [Serializable]
    public class DrawDgree : DrawRectangle
	{
        public int DriviceID { get; set; }   //设备ID
        public int DgreeValue { get; set; }   //电池电量

		public DrawDgree() : this(0, 0, 1, 1)
		{
            //DgreeValue = 20;
		}

        public DrawDgree(int x, int y, int width, int height)
            : base()
        {
            //DgreeValue = 20;
            Rectangle = new Rectangle(x, y, width, height);
            //Rectangle = new Rectangle(x, y, 20, 30);
            Initialize();
        }

        /// <summary>
        /// Clone this instance
        /// </summary>
        public override DrawObject Clone(DrawObject drawObject)
        {
            base.Clone(drawObject);
            return this;
        }

        // 重载属性保存方法
        public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, 
            int orderNumber)
        {
            info.AddValue("driviceID" + orderNumber.ToString(), DriviceID);
            info.AddValue("DgreeValue" + orderNumber.ToString(), DgreeValue);

            base.SaveToStream(info, orderNumber);
        }

        public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info,
            int orderNumber)
        {
            DriviceID = info.GetInt32("driviceID" + orderNumber.ToString());
            DgreeValue = info.GetInt32("DgreeValue" + orderNumber.ToString());

            base.LoadFromStream(info, orderNumber);
        }

        public override void Draw(Graphics g)
        {           
            try
            {
                Pen pen = new Pen(Color, PenWidth);
                g.DrawImage(Image.FromFile(Common.FunctionClass.getRunPath()
                    + "/Resources/DTSD25.jpg")
                    , DrawRectangle.GetNormalizedRectangle(Rectangle));
                Pen pen2 = new Pen(Color.Blue, PenWidth);

                Rectangle rect = new Rectangle(Rectangle.X + 15, Rectangle.Y + (Rectangle.Height
                    - DgreeValue), 
                    Rectangle.Width / 3, DgreeValue);//定义矩形,参数为起点横纵坐标以及其长和宽
                //单色填充
                SolidBrush b1 = new SolidBrush(Color.Red);//定义单色画刷          
                //单色填充这个矩形
                g.FillRectangle(b1, rect);
                //用图片填充
                TextureBrush b2 = new TextureBrush(Image.FromFile(Common.FunctionClass.getRunPath()
                    + "/Resources/DTSD25.jpg"));
                g.FillRectangle(b2, rect);
                //用渐变色填充
                LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.Red, Color.Black,
                    LinearGradientMode.Horizontal);
                g.FillRectangle(b3, rect);

                g.DrawString(DgreeValue.ToString(), new Font("Verdana", 8),
                    new SolidBrush(Color.Chocolate),
                    rect);

                //g.DrawRectangle(pen2, rect);

                pen.Dispose();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }


	}
}
